<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * @param {number[]} nums
       * @return {void} Do not return anything, modify nums in-place instead.
       */
      var moveZeroes = function (nums) {
        /* for (var i = 0; i < nums.length; i++) {
          if (nums[i] === 0) {
            nums[i] = "";
            nums.unshift(0);
            // 在头部前面加了一个，索引需要加上一个
            i++;
          }
        }
        for (var j = 0; j < nums.length; j++) {
          if (nums[j] !== 0 && nums[j - 1] === 0) {
            nums.push(...nums.splice(0, j));
          }
        }
        for (var j = 0; j < nums.length; j++) {
          if (nums[j] === "") {
            nums.splice(j, 1);
            // 删除一个，索引减少一个
            j--;
          }
        } */
        for (var i = 0; i < nums.length; i++) {
          if (nums[i] === 0) {
            nums.splice(i, 1);
            // 删除一个后，减少索引，保证每一个都能遍历到
            i--;
            // 在数组后面添加空串，保证数组长度不变
            nums.push("");
          }
          if (nums[i] === "") {
            for (var j = i; j < nums.length; j++) {
              nums[j] = 0;
            }
            break;
          }
        }
        return nums;
      };

      var nums = [0, 0];
      document.write(JSON.stringify(moveZeroes(nums)));
    </script>
  </body>
</html>
